﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.ComponentModel;
using System.IO;
using TA.JSON;

namespace TA.Turbo.Controls
{
	[PersistChildren(false)]
	[ParseChildren(true)]
	public class GroupedRepeater : Repeater
	{
		#region PUBLIC
		#region INIT
		#endregion
		#region EVENTS
		#endregion
		#region PROPS
		#region DEF
		protected static new Properties _clientProperties = Define(Repeater._clientProperties,
			new BasePropertyDefinition[]
			{
				new ListJsonPropertyDefinition<RepeaterGroupingCollection>("Groupings", "G", null)
				{
					IsHTML = true,
				},
				new EnumPropertyDefinition<GroupedPagingMode>("GroupedPaging", "GP", GroupedPagingMode.PageEntriesThenGroup),
			});
		public override Properties ClientProperties
		{
			get
			{
				return _clientProperties;
			}
		}
		#endregion
		/// <summary>
		/// Gets or sets the collection of groupings which are used to group entries when displaying them on the client.
		/// </summary>
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public RepeaterGroupingCollection Groupings
		{
			get
			{
				return this.GetProperty<RepeaterGroupingCollection>("Groupings");
			}
		}
		/// <summary>
		/// Gets or sets a flag which controls how grouping is performed in a paged <see cref="Repeater"/>.
		/// </summary>
		[Obsolete("This property is not implemented yet.", true)]
		[DefaultValue(GroupedPagingMode.PageEntriesThenGroup)]
		public GroupedPagingMode GroupedPaging
		{
			get
			{
				return this.GetProperty<GroupedPagingMode>("GroupedPaging");
			}
			set
			{
				this.ViewState["GroupedPaging"] = value;
			}
		}

		#endregion
		#region METHODS
		#endregion
		#endregion
		#region INTERNAL
		#region CONST/STATIC
		#endregion
		#region VARS
		#endregion
		#region PROPS
		#endregion
		#region METHODS
		#region INIT
		/// <summary>
		/// Registers "GroupedRepeater.js".
		/// </summary>
		protected internal override void RegisterIncludes(IncludeCollection includes)
		{
			base.RegisterIncludes(includes);

			includes.Add(Script.GroupedPagingMode);
			includes.Add(Script.Element_PerformRecursive);
			includes.Add(Script.Array_AddRange);
			includes.Add(new ScriptResourceInclude("TA.Turbo.Res.JS.GroupedRepeater.Grouping.js", typeof(GroupedRepeater)));
			includes.Add(new ScriptResourceInclude("TA.Turbo.Res.JS.GroupedRepeater.Group.js", typeof(GroupedRepeater)));
			includes.Add(new ScriptResourceInclude("TA.Turbo.Res.JS.GroupedRepeater.GroupedRepeater.js", typeof(GroupedRepeater)));
		}
		/// <summary>
		/// Init.
		/// </summary>
		/// <param name="e"></param>
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);


		}
		/// <summary>
		/// Initializes all of the templates defined by this control.
		/// </summary>
		protected override void Init_Templates()
		{
			base.Init_Templates();

			int index = 0;
			foreach (RepeaterGrouping grouping in this.Groupings)
			{
				grouping.Init(this, index);
				++index;
			}
		}
		/// <summary>
		/// Populates the <see cref="Fields"/> collection with value controls from both <see cref="ViewItem"/> and <see cref="EditItem"/> templates.
		/// </summary>
		protected override void Init_DataFields()
		{
			base.Init_DataFields();

			foreach (RepeaterGrouping grouping in this.Groupings)
			{
				grouping.Header.Populate(this.Fields, path => path.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries).Skip(1).ToArray());
				grouping.Footer.Populate(this.Fields, path => path.Split(new[] { "." }, StringSplitOptions.RemoveEmptyEntries).Skip(1).ToArray());
			}

		}
		/// <summary>
		/// Creates and returns the container control for the data bound items.
		/// </summary>
		/// <returns></returns>
		protected override RepeaterRenderedItemsContainer CreateRenderedItemsContainer()
		{
			return new GroupedRepeaterRenderedItemsContainer(this);
		}
		#endregion
		#region DATA
		#endregion
		#region HANDLERS
		#endregion
		#region RENDER
		/// <summary>
		/// Renders JS init.
		/// </summary>
		/// <param name="writer"></param>
		/// <param name="varName"></param>
		public override void RenderInitScriptInternal(HtmlTextWriter writer, string varName)
		{
			base.RenderInitScriptInternal(writer, varName);

			writer.Write(TPage.PrepareScript(
				string.Format(@"
{0}.set_GroupingsSerialized(",
					varName
				))
			);

			JsonWriter jw = new JsonWriter(writer);
			this.Groupings.Serialize(jw);

			writer.Write(TPage.PrepareScript(@");"));

		}
		#endregion
		#endregion
		#endregion

	}
}
